package com.hiwan.unions.activity.system;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.base.RootActivity;
import com.hiwan.unions.activity.loginRegister.LoginUserActivity;
import com.hiwan.unions.activity.main.MainActivity;
import com.hiwan.unions.application.ConstantsValues;
import com.hiwan.unions.application.exception.ApiException;
import com.hiwan.unions.application.request.CallServer;
import com.hiwan.unions.application.request.ConstsNet;
import com.hiwan.unions.application.request.HttpListener;
import com.hiwan.unions.util.AESKit;
import com.hiwan.unions.util.CheckUtils;
import com.hiwan.unions.util.EditUtils;
import com.hiwan.unions.util.JsonUtil;
import com.hiwan.unions.util.ToastUtils;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONObject;


/**
 * 修改手机号
 */
public class UpdatePhoneActivity extends RootActivity implements HttpListener<JSONObject> {
    private EditText edit_password, yzm_et, edit_phone;
    private Button yzm_button;
    private String password, yzmString, mMobile;
    private Request<JSONObject> request;
    boolean eyeOpen = false;
    private ImageView imageView;

    @Override
    protected int getLayout() {
        return R.layout.activity_update_phone;
    }

    @Override
    protected void initData() {
        bundle = getIntent().getExtras();

    }

    @Override
    protected void onViewCreated() {
        super.onViewCreated();
        edit_phone = findView(R.id.edit_phone);
        yzm_et = findView(R.id.yzm_et);
        yzm_button = findView(R.id.yzm_button);
        EditUtils.phoneNumAddSpace(edit_phone);
        edit_password = findView(R.id.edit_password);
        setTitleTxt("修改手机号");
        imageView = findView(R.id.image);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                eyeOpen = EditUtils.YjImage(imageView, edit_password, eyeOpen);
            }
        });
    }

    public void ButtonOnClick(View view) {
        try {
            mMobile = CheckUtils.checkMobile(edit_phone);
        } catch (ApiException e) {
            ToastUtils.showToast(this, e.getMessage());
            return;
        }
        request = NoHttp.createJsonObjectRequest(ConstsNet.updateMobileCaptcha, RequestMethod.POST);
        request.add("newMobile", mMobile);
        CallServer.getRequestInstance().addToken(this, 0, request, this, true, userInfo);

    }

    private CountDownTimer timerButton = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            yzm_button.setEnabled(false);
            yzm_button.setText((millisUntilFinished / 1000) + "s");
        }

        @Override
        public void onFinish() {
            yzm_button.setEnabled(true);
            yzm_button.setText("重新获取");
        }
    };

    @Override
    protected void IntentConfirm() {
        super.IntentConfirm();
        try {
            password = CheckUtils.checkUpdatePhonePaaword(edit_password);
            mMobile = CheckUtils.checkUpdateMobile(edit_phone, userInfo.getString(ConstantsValues.phone, ""));
            yzmString = CheckUtils.checkYzm(yzm_et);
        } catch (ApiException e) {
            ToastUtils.showToast(this, e.getMessage());
            return;
        }
        request = NoHttp.createJsonObjectRequest(ConstsNet.updateMobileReset, RequestMethod.POST);
        request.add("mobile", mMobile);
        request.add("captcha", yzmString);
        request.add("password", password);
        CallServer.getRequestInstance().addToken(this, 1, request, this, true, userInfo);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timerButton != null) {
            timerButton.cancel();
            timerButton = null;
        }
    }

    @Override
    public void onSucceed(int what, Response<JSONObject> response) {
        JSONObject jsonObject = response.get();
        jsonObject = JsonUtil.getJSONObject(jsonObject, "data");
        ToastUtils.showToast(this, JsonUtil.getString(jsonObject, "msg"));
        SharedPreferences.Editor editor = userInfo.edit();
        switch (what) {
            case 0:
                editor.putString(ConstantsValues.AUTHTOKEN, AESKit.encryptAndBase64(JsonUtil.getString(jsonObject, "authtoken"), ConstantsValues.ASEKIPASSWORD));
                editor.commit();
                timerButton.start();
                break;
            case 1:
                editor.clear();
                editor.putBoolean(ConstantsValues.IS_HASLOGIN, false);
                editor.commit();
                ToastUtils.showToast(this, "修改成功");
                Intent mainIntent = new Intent(this, MainActivity.class);
                mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Intent detailIntent = new Intent(this, LoginUserActivity.class);
                Intent[] intents = {mainIntent, detailIntent};
                startActivities(intents);
                break;
        }

    }

    @Override
    public void onFailed(int what, int errCode, String networkMillis, boolean type) {

    }
}

